/*
 *  This file is part of Bracket Security
 *  Copyright 2011-2013 David R. Smith
 *
 */
package asia.redact.bracket.security;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.security.SecureRandom;

/**
 * Tool to make some uniform message data
 * 
 * @author Dave
 *
 */

public class CreateMessageData {
	
	final int LINES;
	final String path;
	final int TOP, BOTTOM, LINE_LENGTH = 100;
	
	final SecureRandom random = new SecureRandom();
	
	/**
	 * ASCII
	 * @param path
	 * @param lines
	 */
	public CreateMessageData(String path, int lines) {
		this.path = path;
		this.LINES = lines;
		this.TOP = 127;
		this.BOTTOM = 32;
	}
	
	
	public CreateMessageData(String path, int lines, int bottom, int top) {
		this.path = path;
		this.LINES = lines;
		this.TOP= top;
		this.BOTTOM = bottom;
	}
	
	private String createLine() {
		
		if(BOTTOM >= TOP) throw new RuntimeException("Get your numbers right.");
		
		StringBuffer buf = new StringBuffer();
		for(int i = 0; i<LINE_LENGTH;i++) { // for this many char
			int ch = 0;
			while(ch <BOTTOM){
				ch = random.nextInt(TOP);
			}
			
			buf.append((char)ch);
		}
		buf.append("\\");
		buf.append("\n");
		return buf.toString();
	}
	
	public void createData() throws IOException {
		File file = new File(path);
		OutputStream out = new FileOutputStream(file);
		OutputStreamWriter writer = new OutputStreamWriter(out,Charset.forName("UTF-8"));
		
		for(int i = 0; i<LINES;i++) { // for this many lines of data
			writer.append(createLine());
		}
		
		writer.close();
		
	}
	
	public static void main(String [] args) throws FileNotFoundException {
		CreateMessageData data = new CreateMessageData("./src/test/resources/message-data0.ascii", 1024);
		try {
			data.createData();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// http://www.unicode.org/charts/PDF/U3040.pdf
		data = new CreateMessageData("./src/test/resources/message-data-hiragana.utf8", 1024,0x3040,0x309F);
		try {
			data.createData();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// http://www.unicode.org/charts/PDF/U3300.pdf
		data = new CreateMessageData("./src/test/resources/message-data-cjk.utf8", 1024,0x3030,0x333FF);
		try {
			data.createData();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
